package com.xiaoshuidi.cloud.module.contract.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Autowired
    private static StringRedisTemplate stringRedisTemplate;

    public RedisUtils(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;

    //查询每天最大的顺序码
    public static String queryMaxCode(String businessType, String key) {
        final Long couponCode = stringRedisTemplate.opsForValue().increment(key);
        stringRedisTemplate.expire(businessType + key, DEFAULT_EXPIRE, TimeUnit.SECONDS);
        return String.format("%05d", couponCode);
    }
}
